package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Division;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.PartBaseInfo;
import com.sl.au.entity.User;
import com.sl.au.service.DivisionService;
import com.sl.au.service.GridAreaInfoService;
import com.sl.au.service.OptionService;
import com.sl.au.service.PartBaseInfoService;

/**
 * 区域网格部件
 * 
 * @author exde
 *
 */
@RestController
@RequestMapping("/ui/dgp")
public class DGPController extends BaseController {

	@Autowired
	private DivisionService divisionService;
	@Autowired
	private GridAreaInfoService gridAreaInfoService;
	@Autowired
	private PartBaseInfoService partBaseInfoService;
	@Autowired
	private OptionService optionService;

	@RequestMapping(value = "/showUnbindPart", method = RequestMethod.POST)
	public Map<String, Object> showUnbindPart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");// 带过来的id
		String con = request.getParameter("con");
		String attr = request.getParameter("attr");
		String typeId = optionService.getRepository().findByGroupAndTitle("部件类别", "视频").get(0).getId();
		map = getUnbindInfos(key, attr, start, length, con, map, typeId);

		return map;
	}

	private Map<String, Object> getUnbindInfos(String key, String attr, int start, int length, String con,
			Map<String, Object> map, String typeId) {
		List<PartBaseInfo> infos = null;
		int count = 0;
		switch (attr) {
		case "root":// 根节点
			// 查询所有部件
			if (StringUtils.hasText(con)) {
				infos = partBaseInfoService.findUnbindPartOnlyCon(con, typeId, start, length);
				count = partBaseInfoService.findUnbindPartCountOnlyCon(con, typeId);
			} else {
				infos = partBaseInfoService.findUnbindPartNone(typeId, start, length);
				count = partBaseInfoService.findUnbindPartCountNone(typeId);
			}
			break;
		case "division":
			Division division = divisionService.getRepository().findOne(key);
			List<GridAreaInfo> grids = gridAreaInfoService.getRepository()
					.findByDivision(division, new PageRequest(0, Integer.MAX_VALUE)).getContent();
			String[] gridIds = null;
			int len = 0;
			if (grids != null && grids.size() > 0) {
				gridIds = new String[grids.size()];
				for (GridAreaInfo g : grids) {
					gridIds[len++] = g.getId();
				}
			}
			// 区域绑定了网格

			if (StringUtils.hasText(con)) {
				infos = partBaseInfoService.findUnbindPartByGridIdsAndCon(start, length, gridIds, con, typeId);
				count = partBaseInfoService.findUnbindPartCountByGridIdsAndCon(gridIds, con, typeId);
			} else {
				infos = partBaseInfoService.findUnbindPartByGridIds(gridIds, start, length, typeId);
				count = partBaseInfoService.findUnbindPartCountByGridIds(gridIds, typeId);
			}

			break;
		case "grid":
			String[] gids = new String[] { key };

			if (StringUtils.hasText(con)) {
				infos = partBaseInfoService.findUnbindPartByGridIdsAndCon(start, length, gids, con, typeId);
				count = partBaseInfoService.findUnbindPartCountByGridIdsAndCon(gids, con, typeId);
			} else {
				infos = partBaseInfoService.findUnbindPartByGridIds(gids, start, length, typeId);
				count = partBaseInfoService.findUnbindPartCountByGridIds(gids, typeId);
			}
			break;
		default:
			/*
			 * String[] partIds = new String[] { key }; if
			 * (StringUtils.hasText(con)) { infos =
			 * partBaseInfoService.findUnbindPartByPartIdsAndCon(start, length,
			 * partIds, con, typeId); count =
			 * partBaseInfoService.findUnbindPartCountByPartIdsAndCon(partIds,
			 * con, typeId); } else { infos =
			 * partBaseInfoService.findUnbindPartByPartIds(partIds, start,
			 * length, typeId); count =
			 * partBaseInfoService.findUnbindPartCountByPartIds(partIds,
			 * typeId); }
			 */
			break;
		}
		map.put("data", infos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping(value = "/showBindPart", method = RequestMethod.POST)
	public Map<String, Object> showBindPart(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");// 带过来的id
		String con = request.getParameter("con");
		String attr = request.getParameter("attr");
		String typeId = optionService.getRepository().findByGroupAndTitle("部件类别", "视频").get(0).getId();
		map = getBindInfos(key, attr, start, length, con, map, typeId);

		return map;
	}

	private Map<String, Object> getBindInfos(String key, String attr, int start, int length, String con,
			Map<String, Object> map, String typeId) {
		List<PartBaseInfo> infos = null;
		int count = 0;
		switch (attr) {
		case "root":// 根节点
			// 查询所有部件
			if (StringUtils.hasText(con)) {
				infos = partBaseInfoService.findBindPartOnlyCon(con, typeId, start, length);
				count = partBaseInfoService.findBindPartCountOnlyCon(con, typeId);
			} else {
				infos = partBaseInfoService.findBindPartNone(typeId, start, length);
				count = partBaseInfoService.findBindPartCountNone(typeId);
			}
			break;
		case "division":// 查询指定区域下网格绑定的部件
			Division division = divisionService.getRepository().findOne(key);
			List<GridAreaInfo> grids = gridAreaInfoService.getRepository()
					.findByDivision(division, new PageRequest(0, Integer.MAX_VALUE)).getContent();
			String[] gridIds = null;
			int len = 0;
			if (grids != null && grids.size() > 0) {
				gridIds = new String[grids.size()];
				for (GridAreaInfo g : grids) {
					gridIds[len++] = g.getId();
				}
			}
			// 查询网格下的部件
			if (StringUtils.hasText(con)) {
				infos = partBaseInfoService.findBindPartByGridIdsAndCon(start, length, gridIds, con, typeId);
				count = partBaseInfoService.findBindPartCountByGridIdsAndCon(gridIds, con, typeId);
			} else {
				infos = partBaseInfoService.findBindPartByGridIds(gridIds, start, length, typeId);
				count = partBaseInfoService.findBindPartCountByGridIds(gridIds, typeId);
			}

			break;
		case "grid":
			String[] gids = new String[] { key };
			// 查询网格下的部件
			if (StringUtils.hasText(con)) {
				infos = partBaseInfoService.findBindPartByGridIdsAndCon(start, length, gids, con, typeId);
				count = partBaseInfoService.findBindPartCountByGridIdsAndCon(gids, con, typeId);
			} else {
				infos = partBaseInfoService.findBindPartByGridIds(gids, start, length, typeId);
				count = partBaseInfoService.findBindPartCountByGridIds(gids, typeId);
			}
			break;
		default:
			String[] partIds = new String[] { key };
			if (StringUtils.hasText(con)) {
				infos = partBaseInfoService.findBindPartByPartIdsAndCon(start, length, partIds, con, typeId);
				count = partBaseInfoService.findBindPartCountByPartIdsAndCon(partIds, con, typeId);
			} else {
				infos = partBaseInfoService.findBindPartByPartIds(partIds, start, length, typeId);
				count = partBaseInfoService.findBindPartCountByPartIds(partIds, typeId);
			}
			break;
		}
		map.put("data", infos);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	@RequestMapping("/initTree")
	public Map<String, Object> initTree() {
		Map<String, Object> map = new HashMap<>();
		List<Division> list = divisionService.getAllNodes();
		List<Map<String, Object>> nodeMap = divisionService.getDGPNodes(list);
		map.put("nodes", nodeMap);
		return map;
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView) {
		modelAndView = createLayoutView("dgp/index");
		return modelAndView;
	}

}
